﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Xml.Linq;
using System.ComponentModel;
using System.Resources;
using System.Reflection;
using WikiQuizz.Infrastructure.Resources;

namespace WikiQuizz.Infrastructure.WikiAPIHelper
{
    public class WikiAPIHelperClass
    {
        public static XElement QueryEmbeddedIn(string eiTitle)
        {            
            XElement result;

            string URL = String.Format("{0}?action=query&list=embeddedin&format=xml&eititle={1}&einamespace=0&eilimit=max", Strings.ENDPOINT, eiTitle);
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);

            myRequest.UserAgent = "WikiQuizz";
            myRequest.Method = "GET";
            myRequest.Accept = "text/html";
            
            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());                
            }
          
            return result;
        }

        public static XElement GetQueryCategorieMembers(string eiTitle)
        {
            XElement result;

            string URL = String.Format("{0}?action=query&list=categorymembers&format=xml&cmtitle={1}&cmnamespace=0&cmlimit=max", Strings.ENDPOINT, eiTitle);
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);

            myRequest.UserAgent = "WikiQuizz";
            myRequest.Method = "GET";
            myRequest.Accept = "text/html";
            
            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());                
            }
            
            return result;            
        }
        public static XElement GetWikiPage(int pageId)
        {
            XElement result;

            string URL = String.Format("{0}?action=query&list=embeddedin&format=xml&eititle={1}&eilimit=max", Strings.ENDPOINT, pageId);
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);

            myRequest.UserAgent = "WikiQuizz";
            myRequest.Method = "GET";
            myRequest.Accept = "text/html";

            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());
            }

            return result;
        }

        public static String GetExtract(int pageId)
        {
            XElement result;
                        
            string URL = String.Format("{0}?action=query&prop=extracts&format=xml&exlimit=10&exintro=&explaintext=&pageids={1}", Strings.ENDPOINT, pageId);
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);

            myRequest.UserAgent = "WikiQuizz";
            myRequest.Method = "GET";
            myRequest.Accept = "text/html";

            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());
            }

            XElement res = (from o in result.Descendants(XName.Get("extract"))
                          select o).FirstOrDefault();


            return res.Value;
        }

    }
}
